package com.smart.client.core.listener;

import com.smart.client.core.client.IClient;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.EventLoop;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.lang.NonNull;

import java.util.concurrent.TimeUnit;

/**
 * Created By Li Jiaxi
 * <br/>Date: 2023/4/14 17:23
 * <br/>Description:
 */
@Slf4j
// @Component
@RequiredArgsConstructor
public class ConnectionListener implements ChannelFutureListener {

    private final IClient client;

    @Override
    public void operationComplete(@NonNull ChannelFuture channelFuture) throws Exception {
        if (!channelFuture.isSuccess()) {
            log.warn("-------------客户端重新连接-----------------");
            final EventLoop loop = channelFuture.channel().eventLoop();
            loop.schedule(() -> {
                try {
                    client.reconnect();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }, 1L, TimeUnit.SECONDS);
        }
    }
}
